package com.xinfu.newproject.ui.login_register;


import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.base.BaseFragment;
import com.xinfu.newproject.bean.community.ExistCommunityBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.presenter.ExistCommunityPresenter;
import com.xinfu.newproject.presenter.LoginPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.LoginUtils;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import net.qiujuer.genius.widget.GeniusEditText;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.COMMUNITY;
import static com.xinfu.newproject.contants.Constant.COMMUNITYID;
import static com.xinfu.newproject.contants.Constant.EXISTCOMMUNITY;
import static com.xinfu.newproject.contants.Constant.LOGIN;
import static com.xinfu.newproject.contants.Constant.TENANTID;
import static com.xinfu.newproject.contants.Constant.USERIMAGE;


/**
 *
 */
public class ChildLoginFragment extends BaseFragment implements IView.ILoginView, IView.IExistCommunityView {

    @BindView(R.id.m_pass_edt)
    GeniusEditText pwdRt;

    @BindView(R.id.m_pass_hint)
    TextView m_pass_hint;//

    @BindView(R.id.fragment_login_login_phone_tv)
    TextView phoneTv;

    @BindView(R.id.m_pass_edt_bg)
    View m_pass_edt_bg;//分割线

    @BindView(R.id.m_look)
    ImageView m_look;//显示密码

    @BindView(R.id.m_go_login)
    Button m_go_login;//登录按钮

    @BindView(R.id.head_text)
    TextView titleTv;

    private LoginPresenter loginPresenter = new LoginPresenter(this);
    private ExistCommunityPresenter existCommunityPresenter = new ExistCommunityPresenter(this);

    protected BackHandledInterface mBackHandledInterface;

    private boolean mIsPhoneRight = false;
    private boolean mIsPasswordRight = false;
    private boolean mIsPasswordLook = false;
    private Disposable mDisposable;

    private LoginUtils mLoginUtils;
    //手机号,密码是否正确,是否显示密码

    public ChildLoginFragment() {
    }


    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_login_login;
    }

    @Override
    protected void setUpView() {
        titleTv.setText("");
        refresh();
        mLoginUtils = new LoginUtils(getMContext());
        mLoginUtils.setPasswordListener(pwdRt, m_pass_hint, m_pass_edt_bg, m_look, new LoginUtils.EditPhoneListener() {
            @Override
            public void isPhoneRight(boolean bool) {
                mIsPasswordRight = bool;
                isLogin();
            }
        });

        m_go_login.setEnabled(true);
    }

    @Override
    protected void setUpData() {

    }

    /**
     * 判断填写登陆的空格格式，格式错误的话不让点击登录
     */
    private void isLogin() {
        if (mIsPasswordRight) {
            m_go_login.setEnabled(true);
            m_go_login.setBackgroundResource(R.drawable.business_time_bg);
            m_go_login.setTextColor(getResources().getColor(R.color.colorWhite));
        } else {
            m_go_login.setEnabled(false);
            m_go_login.setBackgroundResource(R.drawable.gray_background);
            m_go_login.setTextColor(getResources().getColor(R.color.location_color));
        }
    }

    @OnClick({R.id.m_look, R.id.m_go_login, R.id.m_forgetPwd, R.id.registerText, R.id.head_back})
    public void onClick(final View view) {
        switch (view.getId()) {
            case R.id.registerText:
//                startActivity(new Intent(act, RegistActivity.class));
                break;
            case R.id.m_go_login:
                loginPresenter.login();
                break;
            case R.id.m_look:
                if (mIsPasswordLook) {
                    //显示为密码****
                    pwdRt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    m_look.setImageResource(R.mipmap.invisible_icon);
                } else {
                    //显示为文本
                    pwdRt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    m_look.setImageResource(R.mipmap.visible_icon);
                }
                pwdRt.setSelection(pwdRt.getText().length());
                mIsPasswordLook = !mIsPasswordLook;
                break;
            case R.id.m_forgetPwd:
//                act.startActivity(new Intent(act, ForgetPwdActivity.class));
                break;
            case R.id.head_back:
                getActivity().finish();
                break;

        }
    }

    private void refresh() {
        phoneTv.setText(getArguments().getString("respose"));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            refresh();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    @Override
    public String getUserName() {
        return phoneTv.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return pwdRt.getText().toString().trim();
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String auditRes() {
        return "1";
    }

    @Override
    public void showToast(String content) {
        BToast.showText(getMContext(), content);
    }

    @Override
    public void closeDispose(Disposable disposable) {
        this.mDisposable = disposable;
    }

    @Override
    public void showProgress() {
        ((LoginActivity) getMContext()).showLoding();
    }

    @Override
    public void hideProgress() {
        ((LoginActivity) getMContext()).hideLoding();
    }

    @Override
    public void getData(ExistCommunityBean existCommunityBean) {
        SPUtils.put(getMContext(), EXISTCOMMUNITY, new Gson().toJson(existCommunityBean));
    }

    String token, userId;
    @Override
    public void toOtherActivity(LoginBean loginBean) {

        if (loginBean.getResult().getToken() != null && loginBean.getResult().getUserInfo().getId() != null) {
            token = loginBean.getResult().getToken();
            userId = loginBean.getResult().getUserInfo().getId();
            existCommunityPresenter.existCommunity();
            if (loginBean.getResult().getUserInfo().getProjectName() != null) {
                SPUtils.put(getMContext(), COMMUNITY, loginBean.getResult().getUserInfo().getProjectName());
                SPUtils.put(getMContext(), COMMUNITYID, loginBean.getResult().getUserInfo().getProjectId());
                SPUtils.put(getMContext(), TENANTID, loginBean.getResult().getUserInfo().getTenantId());
                if (loginBean.getResult().getUserInfo().getAvatar() != null) {
                    SPUtils.put(getMContext(), USERIMAGE, loginBean.getResult().getUserInfo().getAvatar());
                }
            }
        }

        existCommunityPresenter.existCommunity();
        SPUtils.put(getMContext(), LOGIN, new Gson().toJson(loginBean.getResult()));
        getActivity().finish();
    }
}
